package com.lrkj.system.service.impl;

import com.lrkj.common.constant.UserConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.StringUtils;
import com.lrkj.system.domain.Category;
import com.lrkj.system.mapper.CategoryMapper;
import com.lrkj.system.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 分类字典Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-03-24
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询分类字典
     *
     * @param categoryId 分类字典ID
     * @return 分类字典
     */
    @Override
    public Category selectCategoryById(String categoryId) {
        return categoryMapper.selectCategoryById(categoryId);
    }

    /**
     * 查询分类字典列表
     *
     * @param category 分类字典
     * @return 分类字典
     */
    @Override
    public List<Category> selectCategoryList(Category category) {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增分类字典
     *
     * @param category 分类字典
     * @return 结果
     */
    @Override
    @CacheEvict(value = "system.category.treeData", allEntries = true)
    public int insertCategory(Category category) {
        this.getAreaLevel(category);
        category.setCategoryId(category.getCategoryCode());
        category.setCreateTime(DateUtils.getNowDate());
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改分类字典
     *
     * @param category 分类字典
     * @return 结果
     */
    @Override
    @CacheEvict(value = "system.category.treeData", allEntries = true)
    public int updateCategory(Category category) {
        this.getAreaLevel(category);
        category.setUpdateTime(DateUtils.getNowDate());
        return categoryMapper.updateCategory(category);
    }


    private void getAreaLevel(Category category) {
        if (StringUtils.isNotEmpty(category.getParentId())) {
            if (category.getParentId().equals("0")) {
                category.setAreaLevel("0");
            } else {
                Category parentBean = categoryMapper.selectCategoryById(category.getParentId());
                category.setAreaLevel((Integer.valueOf(parentBean.getAreaLevel()) + 1) + "");
            }
        }
    }

    /**
     * 批量删除分类字典
     *
     * @param categoryIds 需要删除的分类字典ID
     * @return 结果
     */
    @Override
    @CacheEvict(value = "system.category.treeData", allEntries = true)
    public int deleteCategoryByIds(String[] categoryIds) {
        return categoryMapper.deleteCategoryByIds(categoryIds);
    }

    /**
     * 删除分类字典信息
     *
     * @param categoryId 分类字典ID
     * @return 结果
     */
    @Override
    @CacheEvict(value = "system.category.treeData", allEntries = true)
    public int deleteCategoryById(String categoryId) {
        return categoryMapper.deleteCategoryById(categoryId);
    }

    @Override
    @Cacheable(value = "system.category.treeData", key = "#categoryId")
    public List<Category> treeData(Category category, String categoryId) {
        List<Category> categories = categoryMapper.selectCategoryList(category);
        return getChildPerms(categories, categoryId);
    }

    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list     分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<Category> getChildPerms(List<Category> list, String parentId) {
        List<Category> returnList = new ArrayList<Category>();
        for (Iterator<Category> iterator = list.iterator(); iterator.hasNext(); ) {
            Category t = (Category) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (parentId.equals(String.valueOf(t.getParentId()))) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }


        return returnList;
    }


    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<Category> list, Category t) {
        // 得到子节点列表
        List<Category> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Category tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<Category> it = childList.iterator();
                while (it.hasNext()) {
                    Category n = (Category) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Category> getChildList(List<Category> list, Category t) {
        List<Category> tlist = new ArrayList<Category>();
        Iterator<Category> it = list.iterator();
        while (it.hasNext()) {
            Category n = (Category) it.next();
            if (n.getParentId().equals(t.getCategoryId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Category> list, Category t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
     * 校验code是否唯一
     *
     * @param bean
     * @return
     */
    @Override
    public String checkCodeUnique(Category bean) {
        Category info = categoryMapper.checkCodeUnique(bean.getCategoryCode());
        String status = UserConstants.UNIQUE;
        if (StringUtils.isEmpty(bean.getCategoryId())) {
            if (StringUtils.isNotNull(info)) {
                status = UserConstants.NOT_UNIQUE;
            }
        } else {
            if (!info.getCategoryId().equals(bean.getCategoryId())) {
                status = UserConstants.NOT_UNIQUE;
            }
        }
        return status;
    }
}
